package cn.iocoder.yudao.module.mzt.controller.admin.fiel;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.mzt.controller.admin.fiel.vo.FileUploadReqVO;
import cn.iocoder.yudao.module.mzt.utill.ImageOverlay;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "门户 App - 文件存储")
@RestController
@RequestMapping("/mzt/file")
@Validated
@Slf4j
public class FilePortalController {

    @Autowired
    private FileApi fileApi;

    @Autowired
    private ResourceLoader resourceLoader;
    @PermitAll
    @GetMapping("/test")
    public CommonResult<String> test() throws Exception {
        FileUploadReqVO uploadReqVO=new FileUploadReqVO();
        uploadReqVO.setFile(new ImageOverlay.InMemoryMultipartFile(Files.readAllBytes(new File("C:\\Users\\Administrator\\Desktop\\Java\\1569234847127.jpg").toPath()), "1.png"));
       return uploadFile(uploadReqVO);
    }
    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    @PermitAll
    public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
        String path = uploadReqVO.getPath();
        int randomNumber = (int)(Math.random() * 8) + 1;
        // 获取背景图文件
        Resource resource = resourceLoader.getResource("classpath:templates/images/laptop-template" + randomNumber + ".png");
        // 将 Resource 转换为 MultipartFile
        MultipartFile resourceFile = convertResourceToMultipartFile(resource);
        // 合成图像
        MultipartFile file = ImageOverlay.overlayImages(resourceFile, uploadReqVO.getFile(), 266, 176);
        // 上传文件
        return success(fileApi.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
    }

    private MultipartFile convertResourceToMultipartFile(Resource resource) throws IOException {
        // 将 Resource 转换为临时文件
        File tempFile = File.createTempFile("temp", null);
        try (InputStream inputStream = resource.getInputStream()) {
            Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            return new ImageOverlay.InMemoryMultipartFile(Files.readAllBytes(tempFile.toPath()), tempFile.getName());
        } catch (IOException e) {
            tempFile.delete(); // Ensure the temp file is deleted on error
            throw new IOException("Failed to convert Resource to MultipartFile: " + e.getMessage(), e);
        }
    }
}
